package com.h3bpm.web.instance.consultingOutcomeDocument;

import com.alibaba.fastjson.JSONObject;
import com.h3bpm.web.common.utils.SqlUtil;
import com.h3bpm.web.instance.consultingOutcomeDocument.dao.ReAttachteem;
import data.DataRow;
import data.DataTable;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author villwang
 * @date 2020/07/21
 **/
@RestController
@RequestMapping("/Portal/Consult")
public class ConsultingOutController {

    @Autowired
    ReAttachteem reAttachteem;
    private static final Logger logger = LoggerFactory.getLogger(ConsultingOutController.class);
    @RequestMapping("/getReportNo")
    public JSONObject getReportNo(String businessId){
        logger.info("获取报告号————————businessId————————-"+businessId);
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(businessId)){
            return null;
        }
        try {
            String reportNo="";
            String resultDocumentName="";
            String sql="SELECT resultDocumentName,consultationReportNo FROM i_consultingreportnumber \n" +
                    " WHERE businessId='"+businessId+"'";
            DataTable data = null;
            data = SqlUtil.doQuery(sql);
            List<DataRow> entityRows = data.getEntityRows();
            if(!entityRows.isEmpty()&&entityRows.size()>0) {
                reportNo = entityRows.get(0).getString("consultationReportNo");
                resultDocumentName=entityRows.get(0).getString("resultDocumentName");
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("resultDocumentName",resultDocumentName);
            map.put("reportNo",reportNo);
            logger.info("获取报告号—————————reportNo———————-"+reportNo);
            result.put("state", "200");
            result.put("data", map);
            result.put("msg", "success");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
    @RequestMapping("/reAttachteem")
    public JSONObject reAttachteem(String objectId,String name,String businessId){
        logger.info("回写附件————————objectId————name————-"+objectId+name);
        if(StringUtils.isNotBlank(objectId)&&StringUtils.isNotBlank(businessId)){
            String directory = reAttachteem.getDirectory(name, businessId,objectId);
            if(StringUtils.isNotBlank(directory)){
                reAttachteem.reAttachteem(objectId,directory);
            }else {
                logger.info("回写附件————————————————-创建目录失败");
            }
        }
       return null;
    }


}
